import requests
from datetime import datetime
import time
from random import randint
import pandas as pd


def get_history_price(product_ids):
    counter = 0

    for product_id in product_ids:
        id_list = []
        date_list = []
        price_list = []
        url = {
            "url": f"https://item.jd.com/{product_id}.html"
        }
        auth_response = requests.post("https://api.huizhek.com/api/history/v2", json=url)

        url = auth_response.json()['data']['url']
        auth = auth_response.json()['data']['auth']
        print(url, auth)

        cookie = "tfstk=fi4jhoau-KvjNvFSImCrPHzjK731a-_EDCGTt5L26q3x6Rw8ecnVm5zs58ww0IPOnRase8Mvji2N5fwa9o5YQoos5RwsXsDq7Cd0PJTwutj06FgZW9WF8wP0gV0OL9l73hJm_jKA7ALOygXFbvXF8wPAptqq795GiZuEIf3tMmKxy_GZMVhvDRH-2Xh9kV3TB7CSiXD9XALAyYhm9e_Sa1kbGOl2YazeI4PxFeFUyjFJmSkWWFUSfmMdCYT9WzGbZSZgcesZpkDqYY2RzEg_wfwIO-9RFVF7oyi8kTY_d8ZKPbqlChMQXSr3X01Ak7gjhDar4IKslle05mqXs6V-k8qnKmIlEba48cMnVLBYa7MxAlwVEFH4A7eSYzJy84FLV4srdekBv2K6NDYsNv55NhxZle9yEanFOdmxZbJFN_9jymhoNv55NhxiDbcyL_1Wh4C..; Hm_lvt_85f48cee3e51cd48eaba80781b243db3=1717831917,1718092219; _gid=GA1.2.1925498134.1718092220; pc_202361811_jd=1718092219808; acw_tc=784e2c9717180922284027208e2bf1456749740563c4d30dfcc3a9e45976c7; ASP.NET_SessionId=krpf3es35b0xshi4d2vrn3tv; Hm_lvt_01a310dc95b71311522403c3237671ae=1717831921,1718092226; pc_202461811_taobao=1718092227173; 60014_mmbuser=AA9RUgEGV2oEXFZUAAYPU1QFAA9SVg0DB1ZWBFdXAwJdUFEACFlVBw%3d%3d; Hm_lpvt_85f48cee3e51cd48eaba80781b243db3=1718092276; _ga=GA1.1.834563070.1717831918; _ga_1Y4573NPRY=GS1.1.1718092219.8.1.1718092288.0.0.0; Hm_lpvt_01a310dc95b71311522403c3237671ae=1718092289"
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
            "Authorization": auth,
            'Cookie': cookie
        }
        try:
            history_price_response = requests.get(url, headers=headers)
            print(history_price_response.text)
            history_price = history_price_response.json()['data']['datePrice'].split('],[')
            history_price[0] = history_price[0].removeprefix('[')
            history_price[-1] = history_price[-1].removesuffix(']')
            for date_price in history_price:
                data = date_price.split(',')
                id_list.append(product_id)
                date_list.append(timestamp_to_datetime(float(data[0].removesuffix('000'))))
                price_list.append(data[1])
            print(f"{product_id}爬取完成")
            counter += 1
            if counter % 10 == 0:
                time.sleep(30)
        except Exception as e:
            print(e)
            print('请重新获取cookie')
        history_prices = {
            'product_id': id_list,
            'date_time': date_list,
            'price': price_list
        }
        df = pd.DataFrame(history_prices)
        df.to_csv(f'../data_csv/history_price/database.csv', mode='a', index=False)
        time.sleep(randint(1, 3))


def timestamp_to_datetime(timestamp):
    time_struct = datetime.fromtimestamp(timestamp)
    return time_struct.strftime('%Y-%m-%d %H:%M:%S')


if __name__ == '__main__':
    # 读取csv文件
    # df = pd.read_csv('../data_csv/product_id/data_sku_list.csv')

    # 将一列数据转换为字符串数组
    # string_array = df.iloc[99:120]['data_sku'].astype(str).values

    # get_history_price(['4270019', '100044835937'])

    df = pd.read_csv('../data_csv/history_price/database.csv')

    history_price = df.drop_duplicates()
    # history_price = history_price.drop(159)
    #
    history_price.to_csv('../data_csv/history_price/history_price.csv', index=False)
    # print(history_price)
